<template>
  <div class="login">
    <div class="box">
      <h1 align="center">梦学谷会员管理系统</h1>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="55px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="doLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import store from '@/store';
export default {
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  mounted() {},
  methods: {
    doLogin() {
      this.$refs['ruleForm'].validate( async (valid) => {
        if (valid) {
          try{
              let res = await store.dispatch('doLogin',this.ruleForm)
              if(res.data.flag){
                  this.$message.success(res.data.message)
                  this.$router.push('/layout')
              }else{
                  this.$message.error(res.data.message)
              }
          }catch{   
              this.$message.error('请求发生错误')
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.login {
  width: 100%;
  height: 100%;
  background: url("../../assets/login.jpg") no-repeat;
  display: flex;
  align-items: center;
  justify-content: center;
}
.box {
  width: 350px;
  padding: 30px;
  background-color: rgba(255, 255, 255, 0.8);
  border-radius: 15px;
  h1 {
    font-size: 18px;
    font-weight: bold;
    margin-bottom: 20px;
  }
}
</style>
